package io.renren.socket;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.renren.common.exception.RRException;
import io.renren.common.utils.StringUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import io.renren.common.utils.Constant;

public class SendMsgToClent {
    private static Logger logger = LoggerFactory.getLogger(SendMsgToClent.class);
    public static final String CHARSET = "UTF-8";

    public static String sendgetInfo(String action) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,action);
        obj.put(Constant.CMD_MSGID,"");
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,null,null);
    }

    //终端同步时间
    public static String sendsetDevData(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setDevData");
        obj.put(Constant.CMD_MSGID,"");
//        JSONArray array = new JSONArray();
        JSONObject contentobj = new JSONObject();
        JSONObject DevConfigobj = new JSONObject();
        DevConfigobj.put("dataTime",dataTime);
        contentobj.put("DevConfig",DevConfigobj);
//        array.add(contentobj);
        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //修改终端状态
    public static String sendsetDevState(String IP,String devSrc,String devId,String devState) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setDevState");
        obj.put(Constant.CMD_MSGID,"");
//        JSONArray array = new JSONArray();
        JSONObject contentobj = new JSONObject();
        JSONObject DevConfigobj = new JSONObject();
        DevConfigobj.put("devSrc",devSrc);
        DevConfigobj.put("devId",devId);
        DevConfigobj.put("devState",devState);

        contentobj.put("DevConfig",DevConfigobj);
//        array.add(contentobj);
        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置终端权限
    public static String sendsetDevPermission(String devId,String IP,String allowSampling) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setDevPermission");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject DevConfigobj = new JSONObject();
        DevConfigobj.put("allowSampling",allowSampling);

        contentobj.put("DevConfig",DevConfigobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置循环重启时间setCycleRebootTime
    public static String sendsetCycleRebootTime(String devId,String IP,String cycleRebootTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setCycleRebootTime");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject DevConfigobj = new JSONObject();
        DevConfigobj.put("cycleRebootTime",cycleRebootTime);

        contentobj.put("DevConfig",DevConfigobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //获取终端信息getDevInfo
    public static String sendgetDevInfo(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevInfo");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject DevConfigobj = new JSONObject();
//        DevConfigobj.put("cycleRebootTime",dataTime);
//
//        contentobj.put("DevConfig",DevConfigobj);

//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置红外融合参数
    public static String sendsetIrOverlay(String devId,String IP,String irOvSupport,String scale,String x,String y,String alpha) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setIrOverlay");
        obj.put(Constant.CMD_MSGID,"");
//        JSONArray array = new JSONArray();
        JSONObject contentobj = new JSONObject();

        JSONObject IrOverlayobj = new JSONObject();
        IrOverlayobj.put("irOvSupport",irOvSupport);

        JSONObject IrOvParamobj = new JSONObject();
        IrOvParamobj.put("scale",scale);
        IrOvParamobj.put("x",x);
        IrOvParamobj.put("y",y);
        IrOvParamobj.put("alpha",alpha);

        IrOverlayobj.put("IrOvParam",IrOvParamobj);

        contentobj.put("IrOverlay",IrOverlayobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置红外报警（监测区域）参数
    public static String sendsetIrWarning(String devId,String IP,JSONArray array) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setIrWarning");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();

//        JSONArray arrayIrWarning = new JSONArray();
//        for(int i=0;i<2;i++) {
//            JSONObject IrWarningobj = new JSONObject();
//
//            JSONArray arraypoint = new JSONArray();
//            for (int j = 0; j < 4; j++) {
//                JSONObject pointobj = new JSONObject();
//                pointobj.put("x", dataTime);
//                pointobj.put("y", dataTime);
//                arraypoint.add(pointobj);
//            }
//
//            IrWarningobj.put("delta", dataTime);
//            IrWarningobj.put("diffTemp", dataTime);
//            IrWarningobj.put("distance", dataTime);
//            IrWarningobj.put("lineWidth", dataTime);
//            IrWarningobj.put("maxTemp", dataTime);
//            IrWarningobj.put("name", dataTime);
//            IrWarningobj.put("point", arraypoint);
//            IrWarningobj.put("relativeTemp", dataTime);
//            IrWarningobj.put("type", dataTime);
//            IrWarningobj.put("criteriaFlag", dataTime);
//
//            arrayIrWarning.add(IrWarningobj);
//        }
//        contentobj.put("IrWarning",arrayIrWarning);
        contentobj.put("IrWarning",array);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 设置终端常规参数
    public static String sendsetDevParam(String devId,String IP,JSONObject contobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setDevParam");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
//        JSONObject ComDevParamobj = new JSONObject();
//        ComDevParamobj.put("uploadInterval",uploadInterval);
//        ComDevParamobj.put("resolutionPhoto",resolutionPhoto);
//        ComDevParamobj.put("resolutionVideo",resolutionVideo);
//        ComDevParamobj.put("timeVideo",timeVideo);
//        ComDevParamobj.put("resolutionRVC",resolutionRVC);
//        ComDevParamobj.put("timeRVC",timeRVC);
//        ComDevParamobj.put("precisionTemp",precisionTemp);
//        ComDevParamobj.put("resolutionTI",resolutionTI);
//        ComDevParamobj.put("epsilonIR",epsilonIR);
//        ComDevParamobj.put("distanceIR",distanceIR);
//        ComDevParamobj.put("reflectedTemp",reflectedTemp);

        contentobj.put("ComDevParam",contobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //获取终端信息getDevInfo
    public static String sendgetDevParam(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevParam");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject DevConfigobj = new JSONObject();
//        DevConfigobj.put("cycleRebootTime",dataTime);
//
//        contentobj.put("DevConfig",DevConfigobj);

//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 设置局放报警参数
    public static String sendsetJfWarning(String devId,String IP,JSONObject contobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setJfWarning");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("dischargeCapacityJf",dataTime);
//        JfWarningobj.put("dischargeFrequencyJf",dataTime);
//        JfWarningobj.put("dischargeCapacityDeltaJf",dataTime);
//        JfWarningobj.put("dischargeFrequencyDeltaJf",dataTime);
//        contentobj.put("JfWarning",JfWarningobj);
        contentobj.put("JfWarning",contobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 查询局放报警参数getJfWarning
    public static String sendgetJfWarning(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getJfWarning");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("dischargeCapacityJf",dataTime);
//        JfWarningobj.put("dischargeFrequencyJf",dataTime);
//        JfWarningobj.put("dischargeCapacityDeltaJf",dataTime);
//        JfWarningobj.put("dischargeFrequencyDeltaJf",dataTime);
//
//        contentobj.put("JfWarning",JfWarningobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 重置特高频局放底噪setJfNoiseBase
    public static String sendsetJfNoiseBase(String devId,String IP,String noiseBase) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setJfNoiseBase");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject JfWarningobj = new JSONObject();
        JfWarningobj.put("noiseBase",noiseBase);

        contentobj.put("JfWarning",JfWarningobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 查询特高频局放底噪getJfNoiseBase
    public static String sendgetJfNoiseBase(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getJfNoiseBase");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("noiseBase",dataTime);
//
//        contentobj.put("JfWarning",JfWarningobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 设置可见光报警参数
    public static String sendsetCamWarning(String devId,String IP,String camIDSupport) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setCamWarning");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject CameraWarningobj = new JSONObject();
        CameraWarningobj.put("camIDSupport",camIDSupport);

        contentobj.put("JfWarning",CameraWarningobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 查询可见光报警参数getCamWarning
    public static String sendgetCamWarning(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getCamWarning");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("noiseBase",dataTime);
//
//        contentobj.put("JfWarning",JfWarningobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 设置终端 IP 地址
    public static String sendsetDevIP(String devId,String IP,JSONObject IPInfoobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setDevIP");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
//        JSONObject IPInfoobj = new JSONObject();
//        IPInfoobj.put("ipVersion",ipVersion);
//        IPInfoobj.put("ipType",ipType);
//        IPInfoobj.put("ipAddres",ipAddres);
//        IPInfoobj.put("mask",mask);
//        IPInfoobj.put("gateway",gateway);
//        IPInfoobj.put("dns",dns);

        contentobj.put("IPInfo",IPInfoobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);

//        "action": "setDevIP",
//                "msgId":"",
//                "content": {
//            "IPInfo": {
//                "ipVersion": "IPv4",
//                        "ipType": "static",
//                        "ipAddres": "192.168.0.168",
//                        "mask": "255.255.255.0",
//                        "gateway": "192.168.0.254",
//                        "dns": "202.96.209.133"
//            }
//        }
    }

    // 查询 IP 地址信息getDevIP
    public static String sendgetDevIP(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevIP");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("noiseBase",dataTime);
//
//        contentobj.put("JfWarning",JfWarningobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 配置台账信息
    public static String sendsetStandingBook(String devId,String IP,String setStandingBook,JSONObject StandingBookobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setStandingBook");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();

        contentobj.put("StandingBook",StandingBookobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
//        青岛台账配置示例：
//        {
//            "action": "setStandingBook",
//                "msgId":"",
//                "content": {
//            "StandingBook": {
//                "devSrc": "dev",
//                        "devId": "MP01MN1190601000207",
//                        "sbType": "1",
//                        "SbInfo": [
//                {
//                    "sbName": "IR",
//                        "stationName": "220kV团岛变电站",
//                        "stationCode": "06M00000000106294_zf01",
//                        "dutName": "1号220kV主变",
//                        "dutCode": "06M00000000106640_0301",
//                        "tsName": "1号主变低压侧",
//                        "tsCode": "06M00000000106640_0301_002"
//                },
//                {
//                    "sbName": "UHF",
//                        "stationName": "220kV团岛变电站",
//                        "stationCode": "06M00000000106294_zf01",
//                        "dutName": "1号220kV主变",
//                        "dutCode": "06M00000000106640_0301",
//                        "tsName": "1号主变低压侧",
//                        "tsCode": "06M00000000106640_0301_001"
//                }
//]
//            }
//        }
//        }
//        青浦台账配置：
//        {
//            "action": "setStandingBook",
//                "msgId":"",
//                "content": {
//            "StandingBook": {
//                "devSrc": "dev",
//                        "devId": "MP01MN1190601000207",
//                        "sbType": "2",
//                        "SbInfo": [
//                {
//                    "sbName": "OS11",
//                        "devName": "T2000",
//                        "devCode": "ABC123456",
//                        "devType": "22",
//                        "devTypeCode": "TS123456",
//                        "terminalNo": "6624"
//                }
//]
//            }
//        }
//        }
//        SU10 台账配置：
//        {
//            "action":"setStandingBook",
//                "msgId":"",
//                "content":{
//            "StandingBook":{
//                "devSrc":"dev",
//                        "devId":"MP01MN1190601000207",
//                        "sbType":"3",
//                "SbInfo": [
//                {
//                    "sbName":"SU100001",
//                        "stationName":"220kV 团岛变电站",
//                        "stationCode":"06M00000000106294_zf01",
//                        "dutName":"1 号 220kV 主变",
//                        "dutCode":"06M00000000106640_0301",
//                        "tsName":"1 号主变低压侧",
//                        "tsCode":"06M00000000106640_0301_002"
//                },
//                {
//                    "sbName":"SU100002",
//                        "stationName":"220kV 团岛变电站",
//                        "stationCode":"06M00000000106294_zf01",
//                        "dutName":"1 号 220kV 主变",
//                        "dutCode":"06M00000000106640_0301",
//                        "tsName":"1 号主变低压侧",
//                        "tsCode":"06M00000000106640_0301_001"
//                }
//]
//            }
//        }
//        }
    }

    //查询台账信息getStandingBook
    public static String sendgetStandingBook(String IP,String devSrc,String devId) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getStandingBook");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject StandingBookobj = new JSONObject();
        StandingBookobj.put("devSrc",devSrc);
        StandingBookobj.put("devId",devId);

        contentobj.put("StandingBook",StandingBookobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 设置操作人
    public static String sendsetOperate(String devId,String IP,JSONObject OperateInfoobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setOperate");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
//        JSONObject OperateInfoobj = new JSONObject();
//        OperateInfoobj.put("people",people);
//        OperateInfoobj.put("time",time);
        contentobj.put("OperateInfo",OperateInfoobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
//        "OperateInfo": {
//            "people": "江瑞晶",
//            "time": "2019-07-16 15:22:40"
//        }
    }
    // 设置终端组网配置参数
    public static String sendsetNetConfig(String devId,String IP,JSONObject NetConfigobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setNetConfig");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
//        JSONObject NetConfigobj = new JSONObject();
//        NetConfigobj.put("netType",netType);
//        NetConfigobj.put("wifiName",wifiName);
//        NetConfigobj.put("wifiPassword",wifiPassword);
        contentobj.put("NetConfig",NetConfigobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
//        "NetConfig": {
//            "netType": "ethernet",
//                    "wifiName": "Mnai Energy",
//                    "wifiPassword": "mnaie0601"
//        }
    }

    // 抓取终端log
    public static String sendgetDevLog(String devId,String IP,String dataTime) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevLog");
        obj.put(Constant.CMD_MSGID,"");

//        JSONObject contentobj = new JSONObject();
//        JSONObject JfWarningobj = new JSONObject();
//        JfWarningobj.put("noiseBase",dataTime);
//
//        contentobj.put("JfWarning",JfWarningobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    // 读取传感器信息
    public static String sendgetSensorInfo(String IP,String devSrc,String devId) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getSensorInfo");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();
        JSONObject ComDataobj = new JSONObject();
        ComDataobj.put("devSrc",devSrc);
        ComDataobj.put("devId",devId);
        contentobj.put("ComData",ComDataobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
//        "ComData": [
//        {
//            "devSrc": "dev",
//                "devId": "TP01MN2181221000042"
//        }
//]
    }

    // 召测
    public static String sendcmdRtSampling(String IP,JSONArray sendarray,String devId,String purpose,String devSrc) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdRtSampling");
        obj.put(Constant.CMD_MSGID,"");

        JSONObject contentobj = new JSONObject();

        if(sendarray == null) {
            JSONArray ComDataarray = new JSONArray();
//        for(int i=0;i<2;i++) {
            JSONObject ComDataobj = new JSONObject();
            ComDataobj.put("devSrc", devSrc);
            ComDataobj.put("purpose", purpose);
            ComDataobj.put("devId", devId);
            ComDataarray.add(ComDataobj);
//        }
            contentobj.put("ComData", ComDataarray);
        }
        else{
            contentobj.put("ComData", sendarray);
        }

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //坏点修复
    public static String sendcmdIrBPR(String devId,String IP) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdIrBPR");
        obj.put(Constant.CMD_MSGID,"");
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //控制终端实时视频cmdVideoStream
    public static String sendcmdVideoStream(String devId,String IP,JSONObject VideoStreamInfoobj){
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdVideoStream");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("VideoStreamInfo",VideoStreamInfoobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
//        "VideoStreamInfo": {
//            "devSrc": "dev",
//            "devId": "TP01MN2181221000042",
//                    "sid": "1",
//                    "type": "Camera",
//                    "channel": "camera",
//                    "videoAction": "start"
//        }
    }

    //控制终端热点cmdWiFiHotSpots
    public static String sendcmdWiFiHotSpots(String devId,String IP,JSONObject ComCommandobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdWiFiHotSpots");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("ComCommand",ComCommandobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //控制设备延时重启cmdDevReboot
    public static String sendcmdDevReboot(String devId,String IP,JSONObject ComCommandobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdDevReboot");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("ComCommand",ComCommandobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //获取终端文件-通知终端上传文件getDevFile
    public static String sendgetDevFile(String devId,String IP,JSONArray ComFilearray) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevFile");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("ComFile",ComFilearray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //获取终端数据getDevData
    public static String sendgetDevData(String devId,String IP,JSONArray ComDataarray) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getDevData");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("ComData",ComDataarray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //查询 IED 节点信息getIedNodeInfo
    public static String sendgetIedNodeInfo(String devId,String IP,String ComCommandobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getIedNodeInfo");
//        obj.put(Constant.CMD_MSGID,"");
//        JSONObject contentobj = new JSONObject();
//
//        contentobj.put("ComCommand",ComCommandobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置或者编辑从属终端参数setIedSubDevParam
    public static String sendsetIedSubDevParam(String devId,String IP,JSONArray IedSubDevAttrarray ) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setIedSubDevParam");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("IedSubDevAttr",IedSubDevAttrarray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //查询从属终端参数getIedSubDevParam
    public static String sendgetIedSubDevParam(String devId,String IP,String ComCommandobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"getIedSubDevParam");
//        obj.put(Constant.CMD_MSGID,"");
//        JSONObject contentobj = new JSONObject();
//
//        contentobj.put("ComCommand",ComCommandobj);

//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //设置采样频点setIedSampleBand
    public static String sendsetIedSampleBand(String devId,String IP,JSONArray IedSubDevAttrarray) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setIedSampleBand");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("IedSubDevAttr",IedSubDevAttrarray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //新增从设备addIedSubDev
    public static String sendaddIedSubDev(String devId,String IP,JSONArray IedSubDevAttrarray) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"addIedSubDev");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("IedSubDevAttr",IedSubDevAttrarray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //删除从设备delIedSubDev
    public static String senddelIedSubDev(String devId,String IP,JSONArray IedSubDevAttrarray) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"delIedSubDev");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("IedSubDevAttr",IedSubDevAttrarray);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //删除全部从设备delAllIedSubDev
    public static String senddelAllIedSubDev(String devId,String IP,String param) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"delAllIedSubDev");
//        obj.put(Constant.CMD_MSGID,"");
//        JSONObject contentobj = new JSONObject();
//
//        contentobj.put("ComCommand",ComCommandobj);
//
//        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //配置从设备的上传参数setIedSubDevUploadParam
    public static String sendsetIedSubDevUploadParam(String devId,String IP,JSONObject IedSubDevUploadParamobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"setIedSubDevUploadParam");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("IedSubDevUploadParam",IedSubDevUploadParamobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    //召测某个模块cmdRtSamplingMod(召测可见光模块)
    public static String sendcmdRtSamplingMod(String devId,String IP,JSONObject RtSampleModobj) {
        JSONObject obj = new JSONObject();
        obj.put(Constant.CMD_ACTION,"cmdRtSamplingMod");
        obj.put(Constant.CMD_MSGID,"");
        JSONObject contentobj = new JSONObject();

        contentobj.put("RtSampleMod",RtSampleModobj);

        obj.put(Constant.CMD_CONTENT,contentobj);
        return sendJSONtoClent(obj, Constant.AM_PRIVAT,devId,IP);
    }

    public static String sendJSONtoClent(JSONObject setobj, byte am,String devId,String IP) {
        logger.debug("setobj:" + setobj.toJSONString());
//        new String(,"UTF-8");
        logger.debug("默认编码格式："+Charset.defaultCharset());
        sendTaskLoop:
//        for(;;){
            try{
                Map<String, Object> map = GatewayService.getChannels();
                logger.debug("map size:" + map.size());
                if(map.size() == 0)
                {
                    logger.error("无终端连接");
                    throw new RRException("无终端连接，发送失败");
                }
                if(devId == null || devId == ""){
                    logger.error("设备编号不存在" + devId);
                    return "设备编号不存在";
                }
                if(am != 0x00 && am != 0x01){
                    logger.error("不存在的协议类型" + am);
                    return "不存在的协议类型";
                }
                Iterator<String> it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    ChannelHandlerContext ctx = (ChannelHandlerContext)map.get(key);
                    logger.debug("链接的1-key[",key,"]ip[",ctx.channel().remoteAddress(),"]");
//                    logger.info("channel id: " + key);
//                    logger.info("channel: " + ctx.channel().isActive());
//                    String msg = "{\"从站地址\": \"string\",\"协议类型\": 0,\"类型标识\": \"byte\",\"信息对象地址\": \"int或者int[]\",\"遥控类型\": \"byte\",\"输出⽅式\": \"byte\",\"设置值\": \"byte或者byte[]\",\"私有协议消息\": {\"会话编号\": \"string\",\"消息内容\": \"json\"}}";
                    JSONObject obj = new JSONObject();
                    obj.put("协议类型", am);//0x00—104协议，0x01—私有协议
                    obj.put("从站编码", devId );
                    String objJsonStr = "";
                    if(am == Constant.AM_IEC104) {
//                        obj.put("从站地址", IP );//IP地址
//                        obj.put("类型标识", Constant.M_SP_NA_1);
//                        obj.put("信息对象地址", Constant.M_SP_NA_1);//只有类型标志为136才是数组
//                        obj.put("遥控类型", Constant.M_SP_NA_1);
//                        obj.put("输出⽅式", Constant.M_SP_NA_1);
//                        obj.put("设置值", (byte) Constant.M_SP_NA_1);//只有类型标志为136才是数组
                        setobj.put("协议类型", am);
                        objJsonStr = setobj.toJSONString();
                        logger.debug("发送104数据obj:" + setobj);
                        logger.debug("发送数据objJsonStr:" + objJsonStr);
                    } else if(am == Constant.AM_PRIVAT) {
//                        JSONArray array = new JSONArray();
                        if(IP != null && IP != "") {
                            obj.put("从站地址", IP );//IP地址
                        }else{
                            obj.put("从站地址", "");//IP地址
                            logger.error("IP地址不存在");
                            return "IP地址不存在";
                        }
                        JSONObject obj2 = new JSONObject();//只有私有协议才有该字段
                        obj2.put("会话编号", devId);//会话ID，data区数据所属终端SN号
                        obj2.put("消息内容", setobj);//私有协议消息数据，DATA内容
//                        array.add(obj2);
                        obj.put("私有协议消息", obj2);//只有私有协议才有该字段
                        objJsonStr = obj.toJSONString();
                        logger.debug("发送数据obj:" + obj);
                        logger.debug("发送数据objJsonStr:" + objJsonStr);
                    }



//                    int msglen = StringUtils.stringToHexString(msg).length();
//                    String msglenStr = String.valueOf(msglen);
//                    String unStr = StringUtils.stringToHexString(msg);
//                    logger.debug("unStr:" + unStr);
//                    String msgstrH = StringUtils.stringToHexString(objStr);
                    String msgstrH = StringUtils.toChineseHex(StringEscapeUtils.unescapeJava(objJsonStr));

                    logger.debug("msgstrH:" + msgstrH);
                    //去转义
//                    StringEscapeUtils.unescapeJava(obj.toString());
                    String jsonSize = String.format("%08X", msgstrH.length()/2);
                    String header = "FFFFFFFF" + jsonSize;
//                    byte[] hearByte = StringUtils.hexStringToByteArray(header);
//                    logger.debug("jsonSize:" + jsonSize);
//                    String headerHex = StringUtils.hexStringToString(header);
                    logger.debug("发送的数据:" + header + objJsonStr);
                    logger.debug("发送的数据Hex:" + header + msgstrH);
                    if(ctx.channel().isActive()) {
                        ctx.channel().writeAndFlush(header + msgstrH); // (3)
                    }
                    else {
                        GatewayService.removeGatewayChannel(key);
                    }
// obj.writeAndFlush("hello, it is Server test header ping");
                }
            }catch(Exception e){
                break
                sendTaskLoop;
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.debug("已发送~");
        return null;
//        }
    }

    public static String sendJSON104toClent(JSONObject setobj, byte am,String devId,String IP) {

        logger.debug("setobj:" + setobj.toJSONString());

        sendTaskLoop:
//        for(;;){
        try{
            Map<String, Object> map = GatewayService.getChannels();
            logger.debug("map size:" + map.size());
            if(map.size() == 0)
            {
                throw new RRException("无终端连接，发送失败");
            }
            if(devId == null || devId == ""){
                return "设备编号不存在";
            }
            if(am != 0x00 || am != 0x01){
                return "不存在的协议类型";
            }

            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                ChannelHandlerContext ctx = (ChannelHandlerContext)map.get(key);
                logger.debug("链接的2-key[",key,"]ip[",ctx.channel().remoteAddress(),"]");
                JSONObject obj = new JSONObject();
                if(IP != null && IP != "") {
                    obj.put("从站地址", IP );//IP地址
                }else{
                    obj.put("从站地址", "192.168.1.100");//IP地址
                    return "IP地址不存在";
                }
                obj.put("协议类型", am);//0x00—104协议，0x01—私有协议

                if(am == Constant.AM_IEC104) {
                    obj.put("类型标识", Constant.M_SP_NA_1);
                    obj.put("信息对象地址", Constant.M_SP_NA_1);//只有类型标志为136才是数组
                    obj.put("遥控类型", Constant.M_SP_NA_1);
                    obj.put("输出⽅式", Constant.M_SP_NA_1);
                    obj.put("设置值", (byte) Constant.M_SP_NA_1);//只有类型标志为136才是数组
                } else if(am == Constant.AM_PRIVAT) {
                    JSONObject obj2 = new JSONObject();//只有私有协议才有该字段
                    obj2.put("会话编号", devId);//会话ID，data区数据所属终端SN号
                    obj2.put("消息内容", setobj);//私有协议消息数据，DATA内容
                    obj.put("私有协议消息", obj2);//只有私有协议才有该字段
                }

                String objJsonStr = obj.toJSONString();
                logger.debug("发送数据obj:" + obj);
                logger.debug("发送数据objJsonStr:" + objJsonStr);

                String msgstrH = StringUtils.toChineseHex(StringEscapeUtils.unescapeJava(objJsonStr));

                logger.debug("msgstrH:" + msgstrH);
                //去转义
//                    StringEscapeUtils.unescapeJava(obj.toString());
                String jsonSize = String.format("%08X", msgstrH.length()/2);
                String header = "FFFFFFFF" + jsonSize;
//                    byte[] hearByte = StringUtils.hexStringToByteArray(header);
//                    logger.debug("jsonSize:" + jsonSize);
//                    String headerHex = StringUtils.hexStringToString(header);
                logger.debug("发送的数据:" + header + objJsonStr);
                logger.debug("发送的数据Hex:" + header + msgstrH);
                if(ctx.channel().isActive()) {
                    ctx.channel().writeAndFlush(header + msgstrH); // (3)
                }
                else {
                    GatewayService.removeGatewayChannel(key);
                }
            }
        }catch(Exception e){
            break
                    sendTaskLoop;
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
//        }
    }

    public static String convertStringToUTF8(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        try {
            char c;
            for (int i = 0; i < s.length(); i++) {
                c = s.charAt(i);
                if (c >= 0 && c <= 255) {
                    sb.append(c);
                } else {
                    byte[] b;

                    b = Character.toString(c).getBytes("utf-8");

                    for (int j = 0; j < b.length; j++) {
                        int k = b[j];
                        if (k < 0)
                            k += 256;
                        sb.append(Integer.toHexString(k).toUpperCase());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return sb.toString();
    }
}
